package ArrayList1;

import java.awt.datatransfer.MimeTypeParseException;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: adol book
 * Date: 2022-04-24
 * Time: 17:18
 */
public class MyList {
    public int[] fun;
    public int size;
    private static final int Fun =4;
    public MyList(){
        this.fun = new int[Fun];
    }
    //打印顺序表
    public void diapiay(){
        for (int i = 0; i < this.size; i++) {
            System.out.println(this.fun[i]);
        }
        System.out.println();
    }
    //添加新元素
    public void Add(int a){
        if(isFull()){
           this.fun = Arrays.copyOf(this.fun,2*this.fun.length);
        }else {
            this.fun[this.size] = a;
            this.size++;
        }
    }
    //判断数组满没满
    private boolean isFull(){
        return this.fun.length == this.size;
    }
    private boolean checkPosInAdd(int pos){
        if(pos < 0 || pos > size){
            System.out.println("pos位置不合法");
            return false;
        }
        return true; //合法
    }
    // 在pos 位置新增元素
    public void  Add(int pos,int date) throws MyListindexoutException {
        //下标合法
        if(!checkPosInAdd(pos)){
            throw new MyListindexoutException("添加方法的pos不合理");
        }
        //数组是否满
        if(isFull()){
            this.fun=Arrays.copyOf(this.fun,2*this.fun.length);
        }
        // 挪数据
        for(int i = this.size-1; i >= pos; i--) {
            this.fun[i+1] = this.fun[i];
        }
        //挪完数据
        this.fun[pos] = date;
        this.size++;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.size; i++) {
            if (fun[i] != toFind) {
                return false;
            }
        }
        return true;
        }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.size; i++) {
            if (fun[i] == toFind) {
                return i;
            }
        }
        return -1;
        }
    private boolean checkPosInGet(int pos) {
        if(pos < 0 || pos >= size){
           return false;
        }
        return true; //合法
    }
    private boolean isEmpty(){
        return this.size == 0;
    }
    // 获取 pos 位置的元素
    public int get(int pos) throws MyListindexoutException, MylistEmptyException {
        if(!checkPosInGet(pos)){
            throw new MyListindexoutException("获取pos下标时位置不合法");
        }
        if(isEmpty()){
            throw new MylistEmptyException("获取元素时，顺序表为空");
        }
        return this.fun[pos];
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) throws MyListindexoutException, MylistEmptyException {
        if(!checkPosInGet(pos)){
            throw new MyListindexoutException("更新下标，时位置不合法");
        }
        if(isEmpty()){
            throw new MylistEmptyException("顺序表为空，不能更新");
        }
        this.fun[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) throws MylistEmptyException, MyListindexoutException {
        if(isEmpty()){
            throw new MylistEmptyException("顺序表为空，不能删除");
        }
        int indix = indexOf(toRemove);
        if(indix == -1){
           throw new MyListindexoutException("不存在你想要删除的数据");
        }
        for (int i = indix; i < this.size-1; i++) {
                this.fun[i] = this.fun[i+1];
        }
            this.size--;
        }

    // 获取顺序表长度
    public int size() {
        return this.size;
    }
    // 清空顺序表
    public void clear() {
        this.size = 0;
    }
}